<template>
  <div class="table-box">
    <ProTable ref="proTableRef" row-key="id" title="列表" :columns="columns" :request-api="loadTable" highlight-current-row>
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
        <ElButton type="primary" :icon="Download" @click="excelFunction">导出</ElButton>
      </template>
      <!-- 账户操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton>
        <ElButton type="primary" link :icon="View" @click="openPersonList(scope.row)">人口维护</ElButton>
        <ElButton type="warning" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="danger" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
      </template>
    </ProTable>
    <VillageInfoForm ref="formRef" @success="refresh" />
    <VillagePersonList ref="personListRef" />
  </div>
</template>

<script setup lang="ts" name="ZLVillageInfo">
import { CirclePlus, Delete, EditPen, View, Download } from '@element-plus/icons-vue'
import VillageInfoForm from './village-info-form.vue'
import VillagePersonList from './village-person-list.vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { ref, reactive } from 'vue'
import { VillageInfo, pageVillageInfo, removeVillageInfo } from '@api/tby/community/villageInfo'
import { ElMessage, ElMessageBox } from 'element-plus'
import { FormType } from '@/enums'
import { exportExcel } from '@api/tby/common/common'

const proTableRef = ref<ProTableInstance>()

function refresh() {
  proTableRef.value?.getTableList()
}

function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

// 表格列
const columns = reactive<ColumnProps<VillageInfo.Record>[]>([
  { type: 'index', label: '序号', width: 60, index: getIndex },
  { prop: 'villageName', label: '村庄名称', search: { el: 'input', key: 'villageName', order: 1 }, align: 'center' },
  {
    prop: 'villageType',
    label: '村庄类型',
    enum: VillageInfo.TypeEnum,
    search: { el: 'select', key: 'villageType', order: 1 },
    align: 'center',
    tag: true
  },
  { prop: 'registeredPopulation', label: '户籍人口', align: 'center' },
  { prop: 'permanentPopulation', label: '常驻人口', align: 'center' },
  { prop: 'labourForce', label: '劳动力', align: 'center' },
  { prop: 'longitude', label: '经度', align: 'center' },
  { prop: 'latitude', label: '纬度', align: 'center' },
  { prop: 'operation', label: '操作', fixed: 'right', width: 320 }
])

const loadTable = (query: VillageInfo.Query) => {
  return pageVillageInfo(query)
}

const formRef = ref<InstanceType<typeof VillageInfoForm> | null>(null)
const personListRef = ref<InstanceType<typeof VillagePersonList> | null>(null)

// 添加
const onAddClicked = () => {
  formRef.value?.open(FormType.ADD)
}

// 修改
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 查看用户
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, row.id)
}

// 删除
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('是否删除数据?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    removeVillageInfo(row.id).then(response => {
      console.log(response)
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      proTableRef.value?.getTableList()
    })
  })
}

const openPersonList = (row: any) => {
  personListRef.value?.open(row.id)
}

const excelFunction = () => {
  exportExcel('/village_info/excel', proTableRef.value?.searchParam)
}
</script>
